/**
 * Created by kevin on 16/8/16.
 */

var eosi = window.eosi || {};
this.eosi.fotile = this.eosi.fotile || {};

//this.eosi.fotile = function(name) {
//    this.name = name;
//};
//MYNAMESPACE.person.prototype.getName = function() {
//    return this.name;
//};
(function (window) {
    // the application
    function Fotile() {
        //this.Container_constructor();
        //console.log(this)
        this.init();
    }

    //var p = createjs.extend(Fotile, createjs.Container);

    Fotile.prototype = {
        canvas:null,
        targetWidth:null,//页面宽度高度
        targetHeight:null,//页面宽度高度
        stage:null,
        DEBUG:true,
        fpsLabel:null,
        browserInfo:null,
        hengpingInfo:null,
        alertText:null,
        shuping:true,//是竖屏
        isMaster:true,//是否是主机的判断
        master:null,//主机实例
        loadingMessageField:null,
        container:null,
        init: function () {
            //console.log(this);
            console.log('I am Fitile!!');
            if (createjs.BrowserDetect.isIOS) {
                //alert('ios');
                console.log('IOS');
            }
            else if (createjs.BrowserDetect.isAndroid) {
                //alert('android');
                console.log('android');
            }else{
                //alert('other');
                console.log('other');
            }
            this.canvas = document.getElementById("canvas");
            //console.log(this.canvas.getContext("2d"));
            this.targetWidth = this.getWidth();
            this.targetHeight = this.getHeight();

            //alert(targetWidth);
            this.canvas.width=this.targetWidth;
            this.canvas.height=this.targetHeight;


            this.stage = new createjs.Stage(canvas);
            this.stage.autoClear = true;
            this.container = new createjs.Container();
            this.stage.addChild(this.container);
            //console.log( this.container.getBounds.height);


            //content.regX = 140;
            //content.regY = 140;
            //content.visible = false;

            if(this.DEBUG){
                this.fpsLabel = new createjs.Text("-- fps", "bold 14px Arial", "#000000");
                //stage.addChildAt(fpsLabel,1000);
                this.stage.addChild(this.fpsLabel);
                this.fpsLabel.x = 10;
                this.fpsLabel.y = this.getHeight()-20;

                this.browserInfo = new createjs.Text(detect.viewportWidth()+"x"+detect.viewportHeight(), "bold 14px Arial", "#000000");
                this.stage.addChild(this.browserInfo);
                this.browserInfo.x = 100;
                this.browserInfo.y = this.getHeight()-20;
            }
            //var master = new eosi.fotile.FotileMaster(this);

            window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", this.hengshuping, false);
            if (!createjs.Ticker.hasEventListener("tick")) {
                createjs.Ticker.timingMode = createjs.Ticker.RAF;
                createjs.Ticker.addEventListener("tick", this.tick);
            }

            //createjs.Ticker.on("tick", this.tick, this);
            //fotileAPP.master = new eosi.FotileMaster(fotileAPP);


            //this.master = new eosi.fotile.Master();
        },
        //获取屏幕宽度
        getWidth:function() {
                var xWidth = null;
                if (window.innerWidth !== null) {
                    xWidth = window.innerWidth;
                } else {
                    xWidth = document.body.clientWidth;
                }

                return xWidth;
            },
        //获取屏幕高度
        getHeight:function () {
            var xHeight = null;
            if (window.innerHeight !== null) {
                xHeight = window.innerHeight;
            } else {
                xHeight = document.body.clientHeight;
            }
            return xHeight;
        },
        //判断手机横竖屏状态：
        hengshuping:function (evt){

            if(window.orientation==180||window.orientation==0){
                this.fotileAPP.shuping = true;
                //this.fotileAPP.hideScreenTips();
                //console.log(hideScreenTips)
            }
            if(window.orientation==90||window.orientation==-90){
                this.fotileAPP.shuping = false;
                //this.fotileAPP.showHideScreenTips();
            }
            //console.log(this.fotileAPP);
            this.fotileAPP.showHideScreenTips();
        },
        //横屏提示
        showHideScreenTips:function (){
            //console.log(this.shuping);
            //alert(hengpingInfo);
            if(this.shuping){
                //console.log("竖")
                //console.log(this.hengpingInfo);
                if(this.hengpingInfo!=null){
                    this.stage.removeChild(this.hengpingInfo);
                    this.hengpingInfo = null;
                }

                if(this.alertText!=null){
                    this.stage.removeChild(this.alertText);
                    this.alertText = null;
                }
                //console.log(this.hengpingInfo);

            }else{
                //console.log("横")
                this.hengpingInfo = new createjs.Shape();
                this.hengpingInfo.graphics.clear().beginFill("#333333").drawRect(0,0,this.targetWidth,this.targetHeight);
                this.stage.addChild(this.hengpingInfo);
                this.alertText = new createjs.Text("请竖屏观看", "25px Arial", "#ffffff");
                this.alertText.x = (this.getWidth()-this.alertText.getMeasuredWidth())/2;
                this.alertText.y = (this.getHeight()-this.alertText.getMeasuredHeight())/2;
                this.stage.addChild(this.alertText);
            }
            this.stage.update();


        },
        tick:function (event) {
            if(this.DEBUG){
                this.fpsLabel.text = Math.round(createjs.Ticker.getMeasuredFPS()) + " fps";

            }
            // draw the updates to stage
            this.stage.update(event);
        },

        //加载状态更新
        updateLoading:function (loader) {
            //console.log(this);
            //console.log(loader.target);
            var msg="Loading ";
            //alert(loader.target);
            /*if(loader.target==videoPreloader){
                msg="Loading video ";
            }else if(loader.target == animation_Loader){

                msg="Loading animation ";
            }else if(loader.target == master_UILoader){
                msg="Loading master res ";
            }*/
            //console.log(this.fotileAPP.loadingMessageField);
            //if(loader.target == this.fotileAPP.master.master_UILoader) {
            //    msg = "Loading master res ";
            //}
            console.log(this.fotileAPP)
            if(this.fotileAPP.loadingMessageField == null){
                this.fotileAPP.loadingMessageField = new createjs.Text(msg, "bold 12px Arial", "#000000");
                this.fotileAPP.loadingMessageField.maxWidth = 200;
                this.fotileAPP.loadingMessageField.textAlign = "right";
                this.fotileAPP.loadingMessageField.textBaseline = "middle";
                this.fotileAPP.loadingMessageField.x = this.fotileAPP.canvas.width ;
                this.fotileAPP.loadingMessageField.y = 20;//canvas.height / 2;
                this.fotileAPP.stage.addChild(this.fotileAPP.loadingMessageField);
            }
            if(loader == null){
                this.fotileAPP.loadingMessageField.text = "Nothing is loading!!";
            }else{
                this.fotileAPP.loadingMessageField.text = msg + (loader.progress * 100 | 0) + "%";
            }
            this.fotileAPP.stage.update();
        },
        //clear loading message
        clearLoading:function (){
            if(this.loadingMessageField != null){
                this.stage.removeChild(this.loadingMessageField);
                this.loadingMessageField = null;
                this.stage.update();
            }
        }



    }
    // add MyApp to myNameSpace
    //window.eosi.fotile.Fotile = Fotile;
    window.eosi.fotile.Fotile = createjs.promote(Fotile, "Container");
//}());
}(window));
